import {Component, OnInit} from '@angular/core';
import {FormBuilder, FormGroup, Validators} from '@angular/forms';
import {FetchService} from '../../../core/net/fetch.service';
import {NzMessageService} from 'ng-zorro-antd';
import {Router} from "@angular/router";
import {UploadChangeParam, UploadFile} from "ng-zorro-antd/upload";

export interface Data {
  id: number;
  name: string;
}


@Component({
  selector: 'app-files-version-list',
  templateUrl: './files-version-list.component.html',
  styleUrls: ['./files-version-list.component.scss']
})
export class FilesVersionListComponent implements OnInit {
  searchForm1: FormGroup;
  searchForm2: FormGroup;
  tableData: Data[] = [];
  tableData2 = [];
  yearOption = [
    {value: '', label: '全部'},
    {value: 2019, label: '2019'},
    {value: 2020, label: '2020'},
  ];
  typeOption = [
    {value: '', label: '全部'},
    {value: '1', label: '类型一'},
    {value: '2', label: '类型二'},
  ];
  form: FormGroup;
  fileList: UploadFile[] = [];
  uploadUrl = `${(window as any).environment.apiBaseUrl}comm/file/upload`;
  fileUploadForm: FormGroup;
  fileModalTitle = '文件上传';
  fileModalShow = false;
  uploadFileId = '';
  nzBodyStyle: any = {
    padding: '16px 20px 0'
  };
  selectedId = '';
  constructor(private fb: FormBuilder,
              private fetchService: FetchService,
              private router: Router,
              private message: NzMessageService) {
    this.searchForm1 = this.fb.group({
      name: [''],
      year: ['2019'],
      type: ['']
    });

    this.fileUploadForm = this.fb.group({
      projectId: [null],
      fileName: [null],
      fileTypeName: [null],
      num: [null],
      version: [null]
    });
  }

  ngOnInit(): void {
    this.queryInfo();
  }

  queryInfo(): void {
    this.fetchService.post<any>('archives/wrsProjectInfo/list', {}).subscribe(res => {
      this.tableData = res.list;
    });
  }

  checkDetail(): void {
    this.fetchService.get<any>(`archives/wrsProjectInfo/${this.selectedId}`).subscribe(res => {
      this.tableData2 = res.projectFiles;
    });
  }

  onDelete(id) {
    this.fetchService.post('archives/wrsProjectInfo/delete', {ids: [id]}).subscribe(resp => {
      this.message.info('删除成功');
      this.queryInfo();
    });
  }

  addProj(): void {
    this.router.navigate(['/project-files/project-files-detail'], {queryParams: {id: ''}});
  }

  onConfirm() {
    if (!this.form.valid) {
      this.message.warning('信息填写有误');
      return;
    }
    const data = this.form.value;
    if (data.id) {
      this.fetchService.post('archives/wrsProjectInfo/edit', data).subscribe(res => {
        this.message.success('操作成功！');
      });
    } else {
      this.fetchService.post('archives/wrsProjectInfo/add', data).subscribe(res => {
        this.message.success('操作成功！');
      });
    }
  }

  onFileConfirm(): void {
    if (!this.uploadFileId) {
      this.message.error('请上传附件');
      return;
    }
    const param = {
      projectId: this.fileUploadForm.value.projectId,
      fileId: this.uploadFileId,
      num: this.fileUploadForm.value.num,
      version: this.fileUploadForm.value.version
    };
    this.fetchService.post('archives/wrsProjectFile/add', param).subscribe(res => {
      this.message.success('操作成功！');
      this.fileModalShow = false;
      this.checkDetail();
    });
  }

  onFileCancel(): void {
    this.fileModalShow = false;
  }

  handleChange({file, fileList}: UploadChangeParam): void {
    const status = file.status;
    this.uploadFileId = '';
    if (status !== 'uploading') {
      // console.log(file, fileList);
    }
    if (status === 'done') {
      this.uploadFileId = file.response.data.id;
    } else if (status === 'error') {

    }
  }

  versionManage(item) {
    this.router.navigate(['/project-files/history-versions'], {queryParams: {projectId: item.projectId, num: item.num}});
  }

  cellClick(data): void {
    this.selectedId = data.id;
    this.checkDetail();
  }

  openFileUploadModal(item) {
    this.fileUploadForm.patchValue(item);
    this.fileList = [];
    this.fileModalShow = true;
  }

}
